<?php
 //商品类型控制器
Class TypeAction extends CommonAction {
	 //类型列表模版显示
	Public function index () {
		import('ORG.Util.Page');
		$db = M('type');
		//分页
		$count = $db->count('id');
		$page = new Page($count, 10);
		$limit = $page->firstRow . ',' . $page->listRows;
		$result = $db->limit($limit)->select();
		//分配变量与模板
		$this->type = $result;
		$this->page = $page->show();
		$this->display();
	}


	 //添加类型模板
	Public function addType () {
		$this->display();
	}

	//执行添加类型操作
	Public function runAddType () {
		$data = array(
			'name' => $this->_post('name')
			);
		if (M('type')->data($data)->add()) {
			$this->success('添加成功', U('index'));
		} else {
			$this->error('添加失败');
		}
	}

	//类型属性列表页
	Public function attrList () {
		$tid = (int) $_GET['id'];
		$where = array('tid' => $tid);
		$result = M('type_attr')->where($where)->select();
		$this->attr = $result;
		$this->display();
	}
	 //添加类型属性模板
	Public function addAttr() {
		$this->display();
	}

	 //执行添加类型属性操作
	Public function runAddAttr () {
		$value = str_replace(PHP_EOL, ',', $_POST['value']);
		$data = array(
			'name' => $this->_post('name'),
			'value' => $value,
			'type' => $this->_post('type', 'intval'),
			'tid' => $this->_post('tid', 'intval')
			);
		if (M('type_attr')->data($data)->add()) {
			$this->success('添加成功', U('attrList', array('id' => $_POST['tid'])));
		} else {
			$this->error('添加失败');
		}
	}
}
?>